//
//  SHPStatusCell.swift
//  weibo
//
//  Created by songhuipan on 16/4/21.
//  Copyright © 2016年 songhuipan. All rights reserved.
//

import UIKit
import SnapKit
class SHPStatusCell: UITableViewCell {
    
    var bottomToolbarTopConstraint:Constraint?
    
    var status:SHPStatus? {
        didSet{
            originalView.status = status
            
            bottomToolbarTopConstraint?.uninstall()

            if status?.retweeted_status == nil{

                retweetView.hidden = true
                
                bottomToolbar.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomToolbarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
            }else {
                
                retweetView.hidden = false
                
                retweetView.status = status
                                
                bottomToolbar.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomToolbarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint

                })

            }
            bottomToolbar.status = status
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI () {
        
        self.backgroundColor = getColorWithRGB(237, green: 237, blue: 237)
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomToolbar)


        originalView.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(contentView)
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
        }
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
        }
        bottomToolbar.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            // MARK:如果将这里的约束设置为相对转发微博的底部，会因为此时转发微博的约束没有设置，而崩溃，可以通过Xcode断点和调用堆栈调试
            bottomToolbarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(37)
//            make.bottom.equalTo(contentView)
        }
        // MARK:这里如果不设置contentView的约束，控制台会出现很多错误
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(bottomToolbar)
            make.leading.trailing.top.equalTo(self)
            
        }
    }
    // 懒加载原创微博
    private lazy var originalView:SHPOriginalView = SHPOriginalView()
    // MARK:懒加载底部转发评论赞控件
    private lazy var  bottomToolbar:SHPBottomToolbar = SHPBottomToolbar()
    // MARK:懒加载转发微博控件
    private lazy var retweetView:SHPStatusRetweetView = SHPStatusRetweetView()
}

















